Thread: Arg struct in pThread function [pthread_create()]

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    11

    Unhappy Arg struct in pThread function [pthread_create()]

    I study pThread, and now I have problem with sending arg (struct data type) to new thread...


    In terminal I have this output:
    grytskiv@ZXDSL831II:~/thread/$ gcc -l pthread scaner.c -o scaner && ./scaner
    In main: creating thread 0
    In child: creating thread 0
    �:10072052 ok
    grytskiv@ZXDSL831II:~/thread/$


    but if i call function scanner_port(void *thread_arg) whitout pthread_create(), my function work successful!
    I can't find error...


    Code:
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    #include <pthread.h>
    
    struct address
    {
     char *ip;
     int port;
    };
    
    
    void *
    scanner_port(void *thread_arg)
    {
     struct address *address_pc;
     address_pc = (struct address *) thread_arg;
    
      printf("%s:%i\tok\n",address_pc->ip,address_pc->port);
      exit(0);
    }
    
    
    void *
    scanner_ip(char *ip)
    {
     int port_list[19];
     struct address address_pc;
     address_pc.ip = ip;
    
     port_list[0] = 80;
     port_list[1] = 8080;
    
     pthread_t threads[2];
     int rc;
     long t;
     for(t=0; t<1; t++)
     {
      address_pc.port = port_list[t];
      printf("In child: creating thread %ld\n", t);
      rc = pthread_create(&threads[t],NULL,(void*)scanner_port,(void *) &address_pc);
      if (rc)
      {
       printf("ERROR; return code from pthread_create() is %d\n", rc);
       exit(-1);
      }
      //scanner_port((void *) &address_pc);
     }
    
     pthread_exit(NULL);
    }
    
    
    int
    main()
    {
     char *ip;
     int ip_count = 1;
    
     ip="127.0.0.1";
    
     pthread_t threads[ip_count];
     int rc;
     long t;
     for(t=0; t<ip_count; t++)
     {
      printf("In main: creating thread %ld\n", t);
      rc = pthread_create(&threads[t],NULL,(void*)scanner_ip,ip);
      if (rc)
      {
       printf("ERROR; return code from pthread_create() is %d\n", rc);
       exit(-1);
      }
     }
    
     pthread_exit(NULL);
    
     return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Yes, the problem is, you're passing a pointer to a local variable which is being MODIFIED by the loop calling pthread_create.

    Use malloc to allocate a unique block of memory for each thread you want to create.
    Pass that pointer as the final param to pthread_create()
    In the thread, call free() when you're done with the parameter.

    Yes, the problem is, you're passing a pointer to a local variable which is being MODIFIED by the loop calling pthread_create.

    Use malloc to allocate a unique block of memory for each thread you want to create.
    Pass that pointer as the final param to pthread_create()
    In the thread, call free() when you're done with the parameter.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Help me please.
    By yann in forum C Programming
    Replies: 15
    Last Post: 09-29-2009, 09:04 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

Tags for this Thread